<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>

<jsp:include page="admin_header.jsp"/>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>审核专利</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-hover" id="checkPatent_table">
                <thead>

                <tr>
                    <th>专利名称</th>
                    <th>专利类型</th>
                    <th>所有权人</th>
                    <th>发明人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6" id="page_info_area">

        </div>
        <div class="col-md-6" id="page_nav_area">
        </div>
    </div>

</div>
<script type="text/javascript">

    //1、页面加载完成以后，直接去发送一个ajax请求，要到分页数据
    $(function () {
        //去首页
        to_page(1, "/selAllNewPatent");
    });

    function to_page(pn, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: "pn=" + pn,
            type: "GET",
            success: function (result) {
                //console.log(result);
                //1、解析并显示数据
                build_check_paper(result);
                //2、解析并显示分页数据
                build_page_info(result);
                //3、解析显示分页条数据
                build_page_nav(result, road);
            }
        });
    }

    //1、解析并显示数据
    function build_check_paper(result) {
        //清空table表格
        $("#checkPatent_table tbody").empty();
        var emps = result.data.list;
        $.each(emps, function (index, item) {
            var patentNameTd = $("<td></td>").append(item.patentname);
            var patentTypeTd = $("<td></td>").append(item.patenttype);
            var itemOwnTd = $("<td></td>").append(item.itemown);
            var firstNameTd = $("<td></td>").append(item.firstname);
            var subBtn = $("<button></button>").addClass("btn btn-success btn-sm sub_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-ok"))
                .append("通过");
            subBtn.attr("id", item.resultuuid);
            var failBtn = $("<button></button>").addClass("btn btn-danger btn-sm fail_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-remove")
                    .append("拒绝"));
            failBtn.attr("id", item.resultuuid);
            var btnTd = $("<td></td>").append(subBtn).append(" ").append(failBtn);
            $("<tr></tr>").append(patentNameTd).append(patentTypeTd)
                .append(itemOwnTd).append(firstNameTd)
                .append(btnTd)
                .appendTo("#checkPatent_table tbody");

        });
    }

    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前第" + result.data.pageNum +
            "页，总共" + result.data.pages + "页，总共" + result.data.total + "条记录。");

    }

    //解析显示分页条，点击分页要能去下一页
    function build_page_nav(result, road) {
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");
        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.data.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");

        } else {
            //为元素添加点击翻页的事件
            firstPageLi.click(function () {
                to_page(1, road);
            });

        }
        prePageLi.click(function () {
            to_page(result.data.pageNum - 1, road);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        if (result.data.hasNextPage == false) {
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        } else {
            nextPageLi.click(function () {
                to_page(result.data.pageNum + 1, road);
            });
            lastPageLi.click(function () {
                to_page(result.data.pages, road);
            });
        }
        //添加首页和前一页的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2,3,4,5,遍历给ul中添加页码提示
        $.each(result.data.navigatepageNums, function (index, item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.data.pageNum == item) {
                numLi.addClass("active");
            }
            numLi.click(function () {
                to_page(item, road);
            });
            ul.append(numLi);
        });
        //添加下一页和末页的提示
        ul.append(nextPageLi).append(lastPageLi);
        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }


    //通过审核
    $(document).on("click", ".sub_btn", function () {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 1, "/updatePatent");

    });
    //未通过审核
    $(document).on("click", ".fail_btn", function () {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 2, "/updatePatent");

    });

    function check(patentId, opeCode, road) {
        // alert(paperId);
        $.ajax({
            url: "${APP_PATH}" + road,
            data: {"patentId": patentId + "", "operaCode": opeCode},
            type: "GET",
            success: function (result) {
                /* //console.log(result);
                 //1、解析并显示数据
                 build_check_paper(result);
                 //2、解析并显示分页数据
                 build_page_info(result);
                 //3、解析显示分页条数据
                 build_page_nav(result,road);*/
                to_page(1, "/selAllNewPatent");


            }
        });

    }


</script>


<jsp:include page="admin_foot.jsp"/>
</body>
</html>
